package com.ymatou.iapi.productquery.testcase;

import java.util.Arrays;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productquery.parameter.ProductDescExtra.ProductDescExtraBean;
import com.ymatou.iapi.productquery.service.GetProductDescExtraByProductIdCall;
import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_GetProductDescExtraByProductId {
	private static GetProductDescExtraByProductIdCall getProductDescExtraByProductIdCall = new GetProductDescExtraByProductIdCall();
	
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
	private static String sellerName = EnvSetup.getData("productpriceSellerName");	
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商品新图文描述");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductDescExtraByProductIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDescExtraByProductId_001() {
		Logger.start(true,"查询商品新图文描述,有全量的模板信息");
		try {
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
			
			
			//发布商品
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc12.json");	
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("UserID", sellerId);
			updateMap.put("LoginID", sellerName);
			updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
			updateMap.put("Product->BuyerNoticeId", buyerNoticeTemplate.getTemplateId());
			updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());
			updateMap.put("Product->iThirdCategoryId", 1123);
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);

			//call&获取返回报文
			JSONObject result = SellerProductCallService.addNewProduct(addProductBean);
			String productId = result.getJSONObject("Data").getString("ProductID");
			
			checkDescByProdId(productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDescExtraByProductId_002() {
		Logger.start(true,"查询商品新图文描述,有全量的属性信息");
		try {
			Map<String, Object> thirdCategoryAndPropertyVal = SellerProductCallService.getThirdCategoryAndPropertyValue();
			
			//发布商品
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc12.json");	
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("UserID", sellerId);
			updateMap.put("LoginID", sellerName);
			updateMap.put("Product->iThirdCategoryId", thirdCategoryAndPropertyVal.get("ThirdCategory"));
			updateMap.put("Product->PropertyList", (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);

			//call&获取返回报文
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			checkDescByProdId(productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductDescExtraByProductId_003() {
		Logger.start(true,"查询商品新图文描述,没有模板信息");
		try {
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc10.json");	
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID", sellerName));
			JSONObject result = SellerProductCallService.addNewProduct(addProductBean);
			String productId = result.getJSONObject("Data").getString("ProductID");
			
			checkDescByProdId(productId);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	public static void checkDescByProdId(String prodId) throws Exception{
		JSONObject ret = getProductDescExtraByProductIdCall.search(Arrays.asList(prodId));
		String chkdesc = "GetProductDescExtraByProductId";
		
		ProductDescExtraBean tgtproductDescExtraBean = ProductQueryCallService.getProductDescExtraBean(prodId);
		ProductDescExtraBean actproductDescExtraBean = ProductQueryCallService.getBeanFromJson(
											ret.getJSONObject("Data").getJSONObject("ProductDescExtra"), 
											ProductDescExtraBean.class);
		
		ProductQueryCallService.compareProductDescExtraBean(tgtproductDescExtraBean, actproductDescExtraBean, chkdesc);
	}
	

	
	
	
	
}
